/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrameDiccionario.java
 *
 * Created on 26/05/2012, 10:54:56 PM
 */
package Miscelaneo.TiposGenericos.Demo;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import sun.management.resources.agent;

/**http://javabasico.osmosislatina.com/curso/progfinal2/map.htm
 *
 * @author AMD
 */
public class NewJFrameDiccionario extends javax.swing.JFrame {
    private AgregarPalabra NewPalabra;
    private serializacion serial;
    private ArrayList lista;
    private MiTreeMap mapDiccionario;
    DefaultListModel modeloLista;
    
    /** Creates new form NewJFrameDiccionario */
    public NewJFrameDiccionario() {
        initComponents();
        serial = new  serializacion();
        
        CargarPalabrasA_la_lista();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textAreaSignificado = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        lista_palabras = new javax.swing.JList();
        txt_palabra = new javax.swing.JTextField();
        btn_buscarpalabra = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        btn_agregarPalabra = new javax.swing.JToggleButton();
        btn_salir = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Diccionario     Alumno: Javier Quispe Alcca");
        setResizable(false);

        jLabel1.setBackground(new java.awt.Color(153, 153, 0));
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel1.setForeground(new java.awt.Color(102, 102, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Diccionario de palabras con MAP");

        jPanel3.setBackground(new java.awt.Color(153, 153, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        textAreaSignificado.setColumns(20);
        textAreaSignificado.setEditable(false);
        textAreaSignificado.setRows(5);
        textAreaSignificado.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setViewportView(textAreaSignificado);

        lista_palabras.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lista_palabras.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lista_mouse_click(evt);
            }
        });
        jScrollPane2.setViewportView(lista_palabras);

        txt_palabra.setFont(new java.awt.Font("Tahoma", 0, 14));
        txt_palabra.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        txt_palabra.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btn_txtPalabra_KeyPressed(evt);
            }
        });

        btn_buscarpalabra.setText("Buscar");
        btn_buscarpalabra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarpalabraActionPerformed(evt);
            }
        });

        jLabel2.setText("Todas las palabras:");

        btn_agregarPalabra.setText("Agregar palabra al diccionario");
        btn_agregarPalabra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_agregarPalabraActionPerformed(evt);
            }
        });

        btn_salir.setText("Salir");
        btn_salir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_salirActionPerformed(evt);
            }
        });

        jLabel3.setText("Significado");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 623, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(19, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(txt_palabra, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn_buscarpalabra, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(btn_agregarPalabra, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                        .addComponent(btn_salir, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_palabra, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_buscarpalabra)
                    .addComponent(btn_agregarPalabra)
                    .addComponent(btn_salir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(jLabel1))
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    public void CargarPalabrasA_la_lista(){
        lista = serial.Deserializar("diccionario.dat");
        for (int i = 0; i < lista.size(); i++) {
            if(lista.get(i) instanceof MiTreeMap)
            {
                mapDiccionario = (MiTreeMap)lista.get(i);
                break;
            }
        }
        modeloLista = new  DefaultListModel();
        for( Iterator it = mapDiccionario.keySet().iterator(); it.hasNext();) { 
            String clave = (String)it.next();
	    modeloLista.addElement(clave);
	}
        lista_palabras.setModel(modeloLista);
    }
    
    private void btn_agregarPalabraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_agregarPalabraActionPerformed
        NewPalabra = new AgregarPalabra(mapDiccionario, serial, this);
        NewPalabra.setVisible(true);
        
    }//GEN-LAST:event_btn_agregarPalabraActionPerformed

    private void btn_salirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_salirActionPerformed
               System.exit(0);
        // TODO add your handling code here:
    }//GEN-LAST:event_btn_salirActionPerformed

    private void lista_mouse_click(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lista_mouse_click
         String palabra = (String)lista_palabras.getSelectedValue();
         txt_palabra.setText(palabra);
         String significado = (String)mapDiccionario.get(palabra);
         textAreaSignificado.setText(significado);
    }//GEN-LAST:event_lista_mouse_click

    private void btn_buscarpalabraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarpalabraActionPerformed
         String clave = txt_palabra.getText();
         try {
             Object o = mapDiccionario.get(clave);
             if(o ==null){
               textAreaSignificado.setText("");
               JOptionPane.showMessageDialog(null, "No exite este término...");
             }
             else{
                textAreaSignificado.setText((String)o);  
             }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "No exite este término...");
        }
    }//GEN-LAST:event_btn_buscarpalabraActionPerformed

    private void btn_txtPalabra_KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btn_txtPalabra_KeyPressed
        String subClave = txt_palabra.getText();
        lista_palabras.setSelectedValue(subClave, rootPaneCheckingEnabled);
        
    }//GEN-LAST:event_btn_txtPalabra_KeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(NewJFrameDiccionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrameDiccionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrameDiccionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrameDiccionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NewJFrameDiccionario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btn_agregarPalabra;
    private javax.swing.JButton btn_buscarpalabra;
    private javax.swing.JButton btn_salir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList lista_palabras;
    private javax.swing.JTextArea textAreaSignificado;
    private javax.swing.JTextField txt_palabra;
    // End of variables declaration//GEN-END:variables

    
}
